﻿<div class="k-block divToolbar">
    <div style="display: inline-block">
        <input type="text" id="txtSelProduct" data-value="0" />
    </div>
    <div style="display: inline-block">
        <input type="text" id="txtWarehouse" data-value="0" />
    </div>
    <input type="checkbox" id="cbZero" checked="checked" onchange="Filters()" />隐藏零
</div>
<div class="ld-tabs" id="tab_warehouse">
    <ul>
        <li class="k-state-active">产品总库存</li>
        <li>分仓库存</li>
        <li>分批库存</li>
    </ul>
    <div>
        <div id="grid_repertories_tops" style="text-align: center"></div>
    </div>
    <div>
        <div id="grid_repertories_house" style="text-align: center"></div>
    </div>
    <div>
        <div id="grid_repertories" style="text-align: center"></div>
    </div>
</div>

<script class="init">
    LocatedMenu("仓库管理", "库存情况");

    $("#txtSelProduct").kendoComboBox({
        placeholder: "请选择产品...",
        filter: "contains",
        dataTextField: "Pinyin",
        dataValueField: "ID",
        template: "#=ProductName#",
        dataSource: Whale.dataSource({
            url: "/Products/Product/Gets",
            getParameters: function () {
                return {
                    key: $.trim($("#txtSelProduct").data("kendoComboBox").text())
                };
            },
        }),
        change: function (e) {
            if ($.isNumeric(this.value())) {
                var item = this.dataItem();
                $("#txtSelProduct").attr("data-value", item.ID);
                this.text(item.ProductName);
                Filters();
            } else if (this.text() == "") {
                $("#txtSelProduct").attr("data-value", 0);
                Filters();
            }
        }
    });

    $("#txtWarehouse").kendoComboBox({
        placeholder: "请选择仓库...",
        filter: "contains",
        dataTextField: "House",
        dataValueField: "ID",
        dataSource: Whale.dataSource({
            url: "/Orders/Warehouse/Gets"
        }),
        change: function (e) {
            if (!$.isNumeric(this.value())) {
                $("#txtWarehouse").attr("data-value", 0);
            } else {
                var item = this.dataItem();
                $("#txtWarehouse").attr("data-value", item.ID);
            }
            Filters();
        }
    });
</script>

<script class="grid_repertories_columns">
    var repertoriesFields = [{
        title: "仓库",
        field: "Warehouse.House",
        width: 150
    }, {
        title: "产品名称",
        field: "Product.ProductName",
        width: 150
    }, {
        title: "批号",
        field: "Retention",
        width: 100
    }, {
        title: "库存量",
        field: "Balance",
        template: "#=Balance# (#=Product.UnitName#) #=GetAssitantUnit(Balance, Product)#",
        width: 180
    }, {
        title: "金额",
        field: "FinalCost",
        format: "{0:c}",
        width: 100
    }, {}];

    var repHouseFields = [{
        title: "仓库",
        field: "Warehouse.House",
        width: 150
    }, {
        title: "产品名称",
        field: "Product.ProductName",
        width: 150
    }, {
        title: "库存量",
        field: "Balance",
        template: "#=Balance# (#=Product.UnitName#) #=GetAssitantUnit(Balance, Product)#",
        width: 180
    }, {
        title: "金额",
        field: "FinalCost",
        format: "{0:c}",
        width: 100
    }, {}];

    var repTopsFields = [{
        title: "产品名称",
        field: "Product.ProductName",
        width: 150
    }, {
        title: "库存量",
        field: "Balance",
        template: "#=Balance# (#=Product.UnitName#) #=GetAssitantUnit(Balance, Product)#",
        width: 180
    }, {
        title: "金额",
        field: "FinalCost",
        format: "{0:c}",
        width: 100
    }, {}];
</script>

<script type="text/x-kendo-template" id="detailsTemplate">
    <div class="grid_repertory_details"></div>
</script>

<script class="detail_init">
    function detailInit(e) {
        var detailRow = e.detailRow;
        detailRow.find(".grid_repertory_details").kendoGrid({
            columns: [{
                title: "单据号",
                field: "OrderNo",
                template: "#=GetOperations(OrderNo,Method)#",
                width: 130
            }, {
                title: "产品名称",
                field: "Product.ProductName",
                width: 150
            }, {
                title: "批号",
                field: "Retention",
                width: 100
            }, {
                title: "数量",
                field: "Quantity",
                template: "#=Quantity# (#=Product.UnitName#) #=GetAssitantUnit(Quantity, Product)#",
                width: 150
            }, {
                title: "库存量",
                field: "Balance",
                template: "#=Balance# (#=Product.UnitName#) #=GetAssitantUnit(Balance, Product)#",
                width: 150
            }, {
                title: "金额",
                field: "FinalCost",
                format: "{0:c}",
                width: 100
            }, {
                title: "备注",
                field: "Description",
                width: 200
            }, {
                title: "日期",
                template: "#=kendo.toString(kendo.parseDate(CreatedOn),'yyyy-MM-dd')#",
                width: 100
            }, {

            }],
            dataSource: Whale.dataSource({
                url: "/Orders/Repertory/GetDetails",
                pageSize: 10,
                getParameters: function () {
                    return {
                        productID: e.data.ProductID,
                        houseID: e.data.Warehouse == null ? 0 : e.data.Warehouse.ID,
                        retention: e.data.Retention,
                        getProduct: true,
                        getUnits: true,
                        getHouse: true
                    };
                }
            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            },
        });
    }

    function GetAssitantUnit(quantity, product) {
        var html = "";
        var rate = product.AssistantUnitRate;
        if (rate != null && rate > 0 && product.AssistantUnitName != null) {
            if (quantity < 0) quantity *= -1;
            var d = Math.floor(quantity / rate);
            var m = quantity % rate;
            if (d > 0) {
                html = d + product.AssistantUnitName;
                if (m > 0)
                    html += m + product.UnitName;
            }
        }
        return html;
    }

    function GetOperations(orderNo, method) {
        var html = "";
        if (orderNo != null)
            html = "<a href=\"javascript:ShowProductOrder('" + orderNo + "'," + method + ")\">" + orderNo + "</a>";
        return html;
    }

    function ShowProductOrder(orderNo, method) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "单据详情", width: 800 });
            wndGeneralEdit.refresh({ url: "/Orders/ProductOrder/Order/" + orderNo });
            wndGeneralEdit.open();
        }
    }
</script>

<script class="grid_config">
    $(function () {
        $("#grid_repertories").kendoGrid({
            columns: repertoriesFields,
            dataSource: Whale.dataSource({
                url: "/Orders/Repertory/GetTops",
                getParameters: function () {
                    return {
                        productID: $("#txtSelProduct").attr("data-value"),
                        houseID: $("#txtWarehouse").attr("data-value"),
                        getProduct: true,
                        getUnits: true,
                        getHouse: true,
                        hideZero: $("#cbZero").is(":checked"),
                        repType: 3
                    };
                },
                pageSize: 20

            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            },
            detailTemplate: kendo.template($("#detailsTemplate").html()),
            detailInit: detailInit
        });

        $("#grid_repertories_tops").kendoGrid({
            columns: repTopsFields,
            dataSource: Whale.dataSource({
                url: "/Orders/Repertory/GetTops",
                getParameters: function () {
                    return {
                        productID: $("#txtSelProduct").attr("data-value"),
                        getProduct: true,
                        hideZero: $("#cbZero").is(":checked"),
                        repType: 1
                    };
                },
                pageSize: 20

            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            },
            detailTemplate: kendo.template($("#detailsTemplate").html()),
            detailInit: detailInit
        });

        $("#grid_repertories_house").kendoGrid({
            columns: repHouseFields,
            dataSource: Whale.dataSource({
                url: "/Orders/Repertory/GetTops",
                getParameters: function () {
                    return {
                        productID: $("#txtSelProduct").attr("data-value"),
                        houseID: $("#txtWarehouse").attr("data-value"),
                        getProduct: true,
                        getHouse: true,
                        repType: 2,
                        hideZero: $("#cbZero").is(":checked")
                    };
                },
                pageSize: 20

            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            },
            detailTemplate: kendo.template($("#detailsTemplate").html()),
            detailInit: detailInit
        });
    });

    function Filters() {
        $("#grid_repertories").data("kendoGrid").dataSource.page(0);
        $("#grid_repertories").data("kendoGrid").dataSource.read();
        $("#grid_repertories_house").data("kendoGrid").dataSource.page(0);
        $("#grid_repertories_house").data("kendoGrid").dataSource.read();
        $("#grid_repertories_tops").data("kendoGrid").dataSource.page(0);
        $("#grid_repertories_tops").data("kendoGrid").dataSource.read();
    }
</script>

